//
//  ShopAdvertisingFormViewController.swift
//  iModern
//
//  Created by Ralf on 8/11/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class ShopAdvertisingFormViewController: FormViewController
{

    var quanData2          = NSMutableDictionary()
    var advertisingManager = AdvertisingManager()

    override func viewDidLoad()
    {

//        println(quanData2)

        super.viewDidLoad()
        self.title = "商品管理"
        self.initializeForm()
        self.initBackButton()
        let button = UIButton()
        button.frame = CGRectMake(0, 0, 50, 30)
        button.setTitle("保存", forState: UIControlState.Normal)
        button.setTitleColor(Theme.theme_color, forState: .Normal)
        button.addTarget(self, action: Selector("save"), forControlEvents: UIControlEvents.TouchUpInside)
        let showButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = showButton

    }

    func save()
    {
        let article             = quanData2["article"] as! NSDictionary
        let id:      AnyObject! = article["id"]
        let strVar1: String     = String(stringInterpolationSegment: id)

        let amountOfStocks: AnyObject! = self.formValues()["shopInventory"]

        let intVal = (amountOfStocks as! NSString).integerValue;

        let rs = advertisingManager.updateAdvertising(strVar1, amountOfStocks: intVal)

        if rs == nil
        {
            self.alertNetworkConnectionException()
        }

        if rs!.hasErrors()
        {
            if rs?.errorCode == 3020
            {
                alertUserStatusException()
                NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
                self.view.endEditing(true)
                navigationController?.popToRootViewControllerAnimated(true)
            }
            alertErrorMessage(rs!.errorMessage)
        }
        self.view.endEditing(true)
        appDelegate.contactManager.loadContact()
        self.navigationController?.popViewControllerAnimated(true)

    }

    func initializeForm()
    {
        var form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor

        form = XLFormDescriptor(title: "")

        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "classification", rowType: XLFormRowDescriptorTypeText, title: "")
        row.cellConfigAtConfigure["textField.placeholder"] = "分类       匠造饰品  -    配饰"
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)

        let article = quanData2["article"] as! NSDictionary
        let summary = article["summary"] as! String

        row = XLFormRowDescriptor(tag: "shopName", rowType: XLFormRowDescriptorTypeText, title: "商品名称：")
        row.cellConfigAtConfigure["textField.placeholder"] = summary
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)


        let message = article["message"] as! String

        row = XLFormRowDescriptor(tag: "shopDescribe", rowType: XLFormRowDescriptorTypeText, title: "商品描述：")
        row.cellConfigAtConfigure["textField.placeholder"] = message
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)

        let price:   AnyObject! = article["price"]
        let strVar2: String     = String(stringInterpolationSegment: price)


        row = XLFormRowDescriptor(tag: "shopPrice", rowType: XLFormRowDescriptorTypeText, title: "商品单价：")
        row.cellConfigAtConfigure["textField.placeholder"] = strVar2
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)


        let amountOfStocks: AnyObject! = article["amountOfStocks"]

//        println(amountOfStocks)
        let strVar1: String = String(stringInterpolationSegment: amountOfStocks)


        row = XLFormRowDescriptor(tag: "shopInventory", rowType: XLFormRowDescriptorTypeText, title: "商品库存：")
        row.cellConfigAtConfigure["textField.placeholder"] = strVar1
        row.required = true
        section.addFormRow(row)


        let startTime = article["startTime"] as? String

        if startTime != nil
        {

            row.cellConfigAtConfigure["textField.placeholder"] = startTime
        }
        row = XLFormRowDescriptor(tag: "makeAppointment", rowType: XLFormRowDescriptorTypeText, title: "预约时间：")
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)

        section = XLFormSectionDescriptor.formSectionWithTitle("更多介绍")
        form.addFormSection(section)

        let pmi = article["pmi"] as? String

        if pmi != nil
        {

            row.cellConfigAtConfigure["textField.placeholder"] = pmi
        }
        row = XLFormRowDescriptor(tag: "shopName", rowType: XLFormRowDescriptorTypeText, title: "制作流程：")
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)


        let pom = article["pom"] as? String
        if pom != nil
        {

            row.cellConfigAtConfigure["textField.placeholder"] = pom
        }


        row = XLFormRowDescriptor(tag: "shopName", rowType: XLFormRowDescriptorTypeText, title: "制作材料：")
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)


        let story = article["story"] as? String
        if story != nil
        {

            row.cellConfigAtConfigure["textField.placeholder"] = story
        }

        row = XLFormRowDescriptor(tag: "shopName", rowType: XLFormRowDescriptorTypeText, title: "特色说明：")
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)

        self.form = form
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()

    }

}
